ALV-Grid aus SALV ermitteln

Die Klasse CL_SALV_TABLE erfreut sich seit Jahren großer Beliebtheit. In erster Linie wahrscheinlich deswegen, weil man ohne Ermittlung des Feldkatalogs oder Vorgabe der zugrunde liegenden Datenstruktur eine Tabelle als Grid anzeigen kann. Das SALV verwendet intern jedoch immer noch die Klasse CL_GUI_ALV_GRID. Und es kann durchaus sein, dass man, nachdem man etwas mit dem SALV programmiert hat, an dieses Objekt herankommen möchte.

Achtung! Änderungen in System ab Release 7.55

Wie das geht, zeige ich dir mit der folgenden Klasse.

Coding

CLASS lcl_access_salv DEFINITION INHERITING FROM cl_salv_model_list FINAL.
  PUBLIC SECTION.
    CLASS-METHODS:
      get_cl_gui_alv_grid IMPORTING io_salv        TYPE REF TO cl_salv_model_list
                          RETURNING VALUE(ro_grid) TYPE REF TO cl_gui_alv_grid.
ENDCLASS.

CLASS lcl_access_salv IMPLEMENTATION.
  METHOD get_cl_gui_alv_grid.
    DATA:lo_salv    TYPE REF TO cl_salv_table.
*--------------------------------------------------------------------*
* Must be bound
*--------------------------------------------------------------------*
    IF io_salv IS NOT BOUND.
      RETURN.
    ENDIF.
*--------------------------------------------------------------------*
* Adapter must be bound
*--------------------------------------------------------------------*
    IF   io_salv->r_controller IS NOT BOUND
      OR io_salv->r_controller->r_adapter IS NOT BOUND.
      MESSAGE 'Minor programming fault: Call GET_GRID_FROM_SALV after SALV->DISPLAY( )!'(001) 
         TYPE 'S' DISPLAY LIKE 'W'.
      lo_salv ?= io_salv.  " Calling method has cl_salv_table typed in interface
      lo_salv->display( ).
    ENDIF.
    IF   io_salv->r_controller IS NOT BOUND
      OR io_salv->r_controller->r_adapter IS NOT BOUND.
      RETURN.  " Still not bound  --> can't do anything
    ENDIF.
*--------------------------------------------------------------------*
* If method not present or wrong return type exception will handle this and we return unbound grid
*--------------------------------------------------------------------*
    TRY.

        " Works for CL_SALV_FULLSCREEN_ADAPTER as well as for CL_SALV_GRID_ADAPTER
        CALL METHOD io_salv->r_controller->r_adapter->('GET_GRID')  
          RECEIVING
            value = ro_grid.
      CATCH cx_root ##CATCH_ALL. " don't dump
        RETURN.
    ENDTRY.
  ENDMETHOD.
ENDCLASS.
Enno Wulff